package com.wondertek.mist.plugins;

import org.apache.cordova.CordovaArgs;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONException;
import org.json.JSONObject;

import com.umeng.socialize.Config;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.wondertek.mist.utils.RUtil;
import com.wondertek.mist.utils.StringUtils;

import android.app.Activity;
import android.util.Log;

public class SharePlugin extends CordovaPlugin{

	final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]{
			SHARE_MEDIA.WEIXIN,
			SHARE_MEDIA.WEIXIN_CIRCLE
	};
	
    

	@Override
	public boolean execute(String action, CordovaArgs args,
			CallbackContext callbackContext) throws JSONException {
        JSONObject json = (JSONObject) args.getJSONObject(0);
		
		Log.i("JSONObject", json.toString());
		String title="";
		try
		{
			title=json.getString("title");
			
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		String text=json.getString("text");
		String url=json.getString("url");
		String image=json.getString("image");
		int media=json.getInt("media");
		
		if(StringUtils.isBlank(text)){
			return false;
		}
		
		UMShareListener umShareListener = new UMShareListener() {
	        @Override
	        public void onResult(SHARE_MEDIA platform) {
	            //Log.d("plat","platform"+platform);
	            Log.d("share", platform + " 分享成功啦");
	            //Toast.makeText(SharePlugin.this.cordova.getActivity(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
	        }

	        @Override
	        public void onError(SHARE_MEDIA platform, Throwable t) {
	        	
	        	 Log.e("share", platform + " 分享失败啦",t);
	            //Toast.makeText(SharePlugin.this.cordova.getActivity(),platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
	        }

	        @Override
	        public void onCancel(SHARE_MEDIA platform) {
	        	Log.d("share", platform + " 分享取消了");
	            //Toast.makeText(SharePlugin.this.cordova.getActivity(),platform + " 分享取消了", Toast.LENGTH_SHORT).show();
	        }
	    };
	    Config.dialog=null;
	    Config.dialogSwitch=false;
	    Activity activity=this.cordova.getActivity();
	    
	    
//	    new ShareAction(activity).setDisplayList( displaylist )
//        .withText( text )
//        .withMedia(new UMImage(activity,image))
//        .withTargetUrl(url)
//        .setListenerList(umShareListener)
//        .open();
	    SHARE_MEDIA shareMedia=null;
	    if(media==1){
	    	shareMedia=SHARE_MEDIA.WEIXIN;
	    }else if(media==2){
	    	shareMedia=SHARE_MEDIA.WEIXIN_CIRCLE;
	    }else{
	    	return false;
	    }
	    
	    new ShareAction(activity).setPlatform(shareMedia)
	    .withTitle(title)
        .withText( text )
        .withMedia(new UMImage(activity,image))
        .withTargetUrl(url)
        //.setListenerList(umShareListener)
        .share();
	    
//	    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); 
//		result.setKeepCallback(true); 
//		callbackContext.sendPluginResult(result);

		return true;
	}
}
